package cn.edu.xidian.untils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

/**
 * 创建数据库工具类
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    //保存实例
    private static DatabaseHelper databaseHelper;

    //创建数据库语句
    final static String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS user(user_id text PRIMARY KEY NOT NULL,user_website text NOT NULL ,user_name text NOT NULL,user_pwd text NOT NULL)";

    public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据库
        db.execSQL(CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //对数据库进行变更的时候，增加表的字段，修改表的结构等
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        //禁用WAL
        db.disableWriteAheadLogging();
    }

    //懒汉式单例模式获取数据库工具类
    public static DatabaseHelper getDatebaseHelper(Context context){
        if(databaseHelper == null){
            databaseHelper = new DatabaseHelper(context,"pwd.db",null,01);//实例化数据库对象
        }

        return databaseHelper;
    }
}
